package com.sofwin.controller;

import com.github.pagehelper.PageInfo;
import com.sofwin.Constants;
import com.sofwin.common.ResponseData;
import com.sofwin.pojo.SysRole;
import com.sofwin.system.RoleService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.xml.ws.Response;

/**
 * @packageName: com.sofwin.controller
 * @user: andyliu
 * @date: 2021/9/28 11:14
 * @email 115176513@qq.com
 * @description: TODO
 */
@Controller
@RequestMapping("/role")
public class RoleController {

    @Reference
    private RoleService roleService;

    /**
     * 跳转到角色列表页（同步请求）
     * @return
     */
    @GetMapping
    public String toRole(){
        return "role/data";
    }

    /**
     * 异步加载角色分页数据
     * @param role
     * @return
     */
    @PostMapping("data")
    @ResponseBody
    public ResponseData roleData(SysRole role, @RequestParam(required = true,defaultValue = "1") Integer pageNumber, @RequestParam(required = true,defaultValue = Constants.DEFAULT_PAGESIZE) Integer pageSize){
        PageInfo<SysRole> page = roleService.queryRoleByPage(role, pageNumber, pageSize);
        return new ResponseData(200,page);
    }

    // 跳转到编辑或新增页面[同步请求]

    // 新增或保存（异步）

    // 删除

    // 授权

    /**
     * 跳转到授权页面
     * 将所有资源信息以树状形式展示
     * @return
     */
    @GetMapping("toPermission")
    public String toPermission(SysRole role, Model model){
        model.addAttribute("role",role);
        return "role/permission";
    }

    /**
     * 1. 所有选中节点的ids  可以在前端构建数组
     * 2.当前页中角色的id
     * @return
     */
    @PostMapping("savePermission")
    @ResponseBody
    public ResponseData savePermission(SysRole role,@RequestParam(name = "ids[]") Integer[] ids){
        boolean status = roleService.saveRolePermission(role,ids);
        if(status)return ResponseData.success("授权成功");
        return new ResponseData(400,"操作失败");
    }

}
